package com.zelma.consumer.service.impl;

import com.alibaba.fastjson.JSON;
import com.zelma.common.utils.R;
import io.swagger.models.auth.In;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelma.common.utils.PageUtils;
import com.zelma.common.utils.Query;

import com.zelma.consumer.dao.RentbuyDao;
import com.zelma.consumer.entity.RentbuyEntity;
import com.zelma.consumer.service.RentbuyService;


@Service("rentbuyService")
@SuppressWarnings("all")
public class RentbuyServiceImpl extends ServiceImpl<RentbuyDao, RentbuyEntity> implements RentbuyService {

    @Autowired
    private RentbuyDao rentbuyDao;

    @Autowired
    private RestHighLevelClient client;

    @Override
    public List<Integer> rentBuyArea() {

        return rentbuyDao.getByAid();

    }

    @Override
    public R buyList() throws IOException {
//        首先获取求购房的信息
        QueryWrapper<RentbuyEntity> w = new QueryWrapper<>();
//        判断状态为1的是购房信息
        w.eq("rentbuy_status",1);

        List<RentbuyEntity> rentbuyEntities = baseMapper.selectList(w);

        if(rentbuyEntities.isEmpty()){
            return R.error().put("msg","没有求购房信息");
        }

        for (RentbuyEntity rentbuyEntity : rentbuyEntities) {
            IndexRequest request = new IndexRequest("buy");
            request.id(rentbuyEntity.getRentbuyId()+"");
            String jsonString = JSON.toJSONString(rentbuyEntity);
            request.source(jsonString, XContentType.JSON);
            IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
        }


        return  R.ok().put("rentbuyEntities",rentbuyEntities);
    }
}